package com.lk.user.controller;

import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.VerifyCodeUtils;
import com.lk.user.dto.ChangePasswordCodeDTO;
import com.lk.user.dto.RegisterSmsCodeDTO;
import com.lk.user.dto.VerificationCodeDTO;
import com.lk.user.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /*@RequestMapping("/img")
    public void getImage(HttpSession session, HttpServletResponse response) throws Exception{
        //生成随机字符串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //将字符串存放到session
        session.setAttribute("verifyCode",verifyCode);
        //将字符串合并到图片中
        //将合成的图片，通过Response响应给前端
        ServletOutputStream outputStream = response.getOutputStream();
        VerifyCodeUtils.outputImage(200,40,outputStream,verifyCode);

    }
*/

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            String base64ImageStr=verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResult(base64ImageStr);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @PostMapping("/sms/register")
    public AjaxResult sendRegisterSmsCode(@RequestBody RegisterSmsCodeDTO dto){
        try {
            verifyCodeService.sendRegisterSmsCode(dto);
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败"+e.getMessage());
        }
    }
    @PostMapping("/sms/binder")
    public AjaxResult sendBinderSmsCode(@RequestBody RegisterSmsCodeDTO dto){
        try {
            verifyCodeService.sendBinderSmsCode(dto);
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败"+e.getMessage());
        }
    }
    @PostMapping("/sms/yzm")
    public AjaxResult verificationCode(@RequestBody VerificationCodeDTO dto){
        try {
            verifyCodeService.verificationCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败"+e.getMessage());
        }
    }
    @PostMapping("/sms/gmm")
    public AjaxResult changePassword(@RequestBody ChangePasswordCodeDTO dto){
        try {
            verifyCodeService.changePassword(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败"+e.getMessage());
        }
    }
}
